package com.practice.Optional语法糖;
import java.util.Optional;

/**
 * Created by IntelliJ IDEA2021.3
 * @Author: Tenghw
 * @Date: 2023/02/24  13:52
 * @Description: Java8新特性之Optional用法
 */
@SuppressWarnings("all")
public class Demo1 {
    public static void main(String[] args) {
        // 依据一个非空值创建Optional
        Optional<Student> optional1 = Optional.of(new Student());
        System.out.println(optional1.isPresent());
        System.out.println(optional1);
        System.out.println(optional1.get());
        System.out.println("------------------------");

        // 声明一个空容器对象Optional
        Optional<Object> optional2 = Optional.empty();
        System.out.println(optional2.isPresent());
        System.out.println(optional2.orElse(new Integer(1)));
        //System.out.println(optional2.get());//会报异常
        System.out.println("------------------------");

        // 可接受null的Optional
        Optional<Object> optional3 = Optional.ofNullable(null);
        System.out.println(optional3.isPresent());


    }
}
